#include "chatview.h"

ChatView::ChatView(QWidget *parent):QWidget(parent),isInserted(false),isAppended(false)
{
    QVBoxLayout *pMainLayout=new QVBoxLayout();
    this->setLayout(pMainLayout);
    pMainLayout->setMargin(0);

    m_pScrollArea=new QScrollArea();
    m_pScrollArea->setObjectName("chat_area");
    pMainLayout->addWidget(m_pScrollArea);

    QWidget* w=new QWidget(this);
    w->setObjectName("chat_bg");
    w->setAutoFillBackground(true);

    QVBoxLayout *pVLayout_1=new QVBoxLayout();
    pVLayout_1->addWidget(new QWidget(),100000);
    w->setLayout(pVLayout_1);
    m_pScrollArea->setWidget(w);

    m_pScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    QScrollBar* pVScrollBar=m_pScrollArea->verticalScrollBar();
    connect(pVScrollBar,&QScrollBar::rangeChanged,this,&ChatView::onVScrollBarMoved);

    //把垂直scrollbar放在上边，而不是原来的并排
    QHBoxLayout *pHLayout_2=new QHBoxLayout();
    pHLayout_2->addWidget(pVScrollBar,0,Qt::AlignRight);
    pHLayout_2->setMargin(0);
    m_pScrollArea->setLayout(pHLayout_2);
    pVScrollBar->setHidden(true);

    m_pScrollArea->setWidgetResizable(true);
    m_pScrollArea->installEventFilter(this);
    iniStyleSheet();
}

void ChatView::bottonInsertChatItem(QWidget *item)
{
    QVBoxLayout* vl=qobject_cast<QVBoxLayout*>(m_pScrollArea->widget()->layout());
    vl->insertWidget(vl->count()-1,item);
    isInserted=true;
}

void ChatView::appendChatItem(QWidget *item)
{
    QVBoxLayout *vl = qobject_cast<QVBoxLayout *>(m_pScrollArea->widget()->layout());
    qDebug() << "vl->count() is " << vl->count();
    vl->insertWidget(vl->count()-1, item);
    isAppended = true;
}

void ChatView::removeAllItem()
{
    QVBoxLayout *layout = qobject_cast<QVBoxLayout *>(m_pScrollArea->widget()->layout());

    int count = layout->count();

    for (int i = 0; i < count - 1; ++i) {
        QLayoutItem *item = layout->takeAt(0); // 始终从第一个控件开始删除
        if (item) {
            if (QWidget *widget = item->widget()) {
                delete widget;
            }
            delete item;
        }
    }
}

bool ChatView::eventFilter(QObject *watched, QEvent *event)
{
    if(event->type()==QEvent::Enter&&watched==m_pScrollArea){
        m_pScrollArea->verticalScrollBar()->setHidden(m_pScrollArea->verticalScrollBar()->maximum()==0);
    }
    else if(event->type()==QEvent::Leave&&watched==m_pScrollArea){
        m_pScrollArea->verticalScrollBar()->setHidden(true);
    }
    return QWidget::eventFilter(watched,event);
}

void ChatView::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&p,this);
}

void ChatView::onVScrollBarMoved(int min, int max)
{
    if(isInserted){
        QScrollBar* pVScrollBar=m_pScrollArea->verticalScrollBar();
        pVScrollBar->setSliderPosition(pVScrollBar->maximum());
        QTimer::singleShot(500,[this](){
            isInserted=false;
        });
    }

}

void ChatView::iniStyleSheet()
{
       QScrollBar *scrollBar = m_pScrollArea->verticalScrollBar();
       scrollBar->setStyleSheet("QScrollBar{background:transparent;}"
                                "QScrollBar:vertical{background:transparent;width:8px;}"
                                "QScrollBar::handle:vertical{background:red; border-radius:4px;min-height:20px;}"
                                "QScrollBar::add-line:vertical{height:0px}"
                                "QScrollBar::sub-line:vertical{height:0px}"
                                "QScrollBar::add-page:vertical {background:transparent;}"
                                "QScrollBar::sub-page:vertical {background:transparent;}");
}

